package com.liveneo.adapter.mobile.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liveneo.adapter.base.entity.PrpmLossAccessory;
import com.liveneo.adapter.base.entity.PrpmLossFit;
import com.liveneo.adapter.base.entity.PrpmLossMain;
import com.liveneo.adapter.base.entity.PrpmLossRepair;
import com.liveneo.adapter.mobile.model.dto.PlatLossSingleDto;
import com.liveneo.adapter.mobile.service.LossAccessoryService;
import com.liveneo.adapter.mobile.service.LossFitService;
import com.liveneo.adapter.mobile.service.LossMainService;
import com.liveneo.adapter.mobile.service.LossRepairService;
import com.liveneo.adapter.mobile.service.MobileSaveModelFromClaimService;
import com.liveneo.adapter.mobile.util.UUIDUtils;

/**
 *@Title:手机保存理赔推送信息实现类
 *@Description:
 *@Author:zhoupan
 *@Since:2015-8-13 下午03:47:40
 *@Version:1.0.0
 *@Copyright (c) 北京朗泰恒盛信息技术有限公司-版权所有
 */
@Service
public class MobileSaveModelFromClaimServiceImpl implements MobileSaveModelFromClaimService {
    @Autowired
    private LossMainService      lossMainService;
    @Autowired
    private LossAccessoryService lossAccessoryService;
    @Autowired
    private LossRepairService    lossRepairService;
    @Autowired
    private LossFitService       lossFitService;

    @Override
    public void saveLossSingleDto(PlatLossSingleDto platLossSingleDto, String lossUuid) throws Exception {
        // TODO Auto-generated method stub
        // 定损单主信息
        if (platLossSingleDto.getLossMain() != null && !"".equals(platLossSingleDto.getLossMain())) {
            if (platLossSingleDto.getLossMain().getLossId() != null && !"".equals(platLossSingleDto.getLossMain().getLossId())) {
                PrpmLossMain prpmLossMain = lossMainService.getprpmLossMain(lossUuid);
                if (prpmLossMain != null && !"".equals(prpmLossMain)) {
                    lossMainService.deleteprpmLossMain(lossUuid);
                }
                platLossSingleDto.getLossMain().setLossMainId(UUIDUtils.getUUIDStr());
                platLossSingleDto.getLossMain().setLossItemId(lossUuid);
                lossMainService.saveLossMain(platLossSingleDto.getLossMain());
            }
        }
        // 辅料信息
        if (platLossSingleDto.getLossAccessoryList() != null && platLossSingleDto.getLossAccessoryList().size() > 0) {
            List<PrpmLossAccessory> prpmLossAccessoryList = lossAccessoryService.getLossAccessoryList(lossUuid);
            if (prpmLossAccessoryList != null && prpmLossAccessoryList.size() > 0) {
                lossAccessoryService.deleteLossAccessory(lossUuid);
            }
            List<PrpmLossAccessory> lossAccessoryList = new ArrayList<PrpmLossAccessory>();
            for(PrpmLossAccessory lossAccessory : platLossSingleDto.getLossAccessoryList()){
            	PrpmLossAccessory lossAccessoryNew = new PrpmLossAccessory();
            	BeanUtils.copyProperties(lossAccessory, lossAccessoryNew);
            	lossAccessoryNew.setLossAccessoryId(UUIDUtils.getUUIDStr());
            	lossAccessoryNew.setLossItemId(lossUuid);
            	lossAccessoryList.add(lossAccessoryNew);
            }
            lossAccessoryService.saveLossAccessory(lossAccessoryList);
        }
        // 换件信息
        if (platLossSingleDto.getLossFitList() != null && platLossSingleDto.getLossFitList().size() > 0) {
            List<PrpmLossFit> prpmLossFitList = lossFitService.getLossFitList(lossUuid);
            if (prpmLossFitList != null && prpmLossFitList.size() > 0) {
                lossFitService.deleteLossFitList(lossUuid);
            }
            List<PrpmLossFit> lossFitList = new ArrayList<PrpmLossFit>();
            for(PrpmLossFit lossFit : platLossSingleDto.getLossFitList()){
            	PrpmLossFit lossFitNew = new PrpmLossFit();
            	BeanUtils.copyProperties(lossFit, lossFitNew);
            	lossFitNew.setLossItemId(lossUuid);
            	lossFitNew.setLossFitId(UUIDUtils.getUUIDStr());
            	lossFitList.add(lossFitNew);
            }
            lossFitService.saveLossFitList(lossFitList);
        }
        // 修理信息
        if (platLossSingleDto.getLossRepair() != null && platLossSingleDto.getLossRepair().size() > 0) {
            List<PrpmLossRepair> prpmLossRepairList = lossRepairService.getLossRepairList(lossUuid);
            if (prpmLossRepairList != null && prpmLossRepairList.size() > 0) {
                lossRepairService.deleteLossRepairList(lossUuid);
            }
            List<PrpmLossRepair> lossRepairList = new ArrayList<PrpmLossRepair>();
            for(PrpmLossRepair lossRepair : platLossSingleDto.getLossRepair()){
            	PrpmLossRepair lossRepairNew = new PrpmLossRepair();
            	BeanUtils.copyProperties(lossRepair, lossRepairNew);
            	lossRepairNew.setLossRepairId(UUIDUtils.getUUIDStr());
            	lossRepairNew.setLossItemId(lossUuid);
            	lossRepairList.add(lossRepairNew);
            }
            lossRepairService.saveLossRepairList(lossRepairList);
        }
    }
}
